#include <xp/bas/stdio.h>
#include <xp/bas/stdlib.h>
#include <xp/bas/locale.h>
#include <xpp/bas/LinkedList.h>

#ifdef __linux
#include <mcheck.h>
#endif

using namespace xpp::bas;

class IntList: public LinkedList<int>
{
public:
	IntList () {}
	IntList (xp_size_t mpb_size): LinkedList<int>(mpb_size) {}

	int print_node_forward (Node* /*start*/, Node* cur)
	{
	        xp_printf (XP_TEXT("%d\n"), cur->value);
		return 1; // traverse forward
	}

	int print_node_backward (Node* /*start*/, Node* cur)
	{
		xp_printf (XP_TEXT("%d\n"), cur->value);
		return -1; // traverse backward
	}
};

extern "C" { int xp_main (int argc, xp_char_t* argv[]); }

int xp_main (int argc, xp_char_t* argv[])
{
#ifdef __linux
	mtrace ();
#endif  

	if (argc > 2) {
		xp_printf (XP_TEXT("usage: %s [memory_block_size]\n"));
		return -1;
	}

	xp_setlocale ();

	int mbp = 0;
	if (argc == 2) XP_STRTOI(mbp, argv[1], XP_NULL, 10);
	IntList list (mbp);

	for (int i = 4; i < 10; i++) list.append (i);
	list.traverse (
		(IntList::TraverseCallback)&IntList::print_node_backward, list.tail());
	xp_printf (XP_TEXT("-----------------\n"));
	list.traverse (
		(IntList::TraverseCallback)&IntList::print_node_forward, list.head());

	return 0;
}

